package com.gt.web.srv.web.api.wx;

import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/wxpay")
public class WechatPayCallbackController {


    @Resource(name = "notificationParser")
    private NotificationParser parser;

    /**
     * 支付结果回调
     */
    @PostMapping("/pay-callback")
    public String handlePayCallback(@RequestBody String body,
                                    @RequestHeader("Wechatpay-Signature") String signature,
                                    @RequestHeader("Wechatpay-Nonce") String nonce,
                                    @RequestHeader("Wechatpay-Serial") String serial,
                                    @RequestHeader("Wechatpay-Timestamp") String timestamp) {
        try {
            // 构造请求参数
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(serial)
                    .nonce(nonce)
                    .signature(signature)
                    .timestamp(timestamp)
                    .body(body)
                    .build();


            // 验签并解密报文
            Transaction transaction = parser.parse(requestParam, Transaction.class);

            // 处理支付结果
            if ("SUCCESS".equals(transaction.getTradeState())) {
//                orderService.handlePaymentSuccess(
//                        transaction.getOutTradeNo(),
//                        transaction.getTransactionId(),
//                        transaction.getAmount().getTotal()
//                );
            }

            return "{\"code\": \"SUCCESS\", \"message\": \"成功\"}";
        } catch (Exception e) {
            return "{\"code\": \"FAIL\", \"message\": \"处理失败\"}";
        }
    }



    /**
     * 退款结果回调
     */
    @PostMapping("/refund-callback")
    public String handleRefundCallback(@RequestBody String body,
                                       @RequestHeader("Wechatpay-Signature") String signature,
                                       @RequestHeader("Wechatpay-Nonce") String nonce,
                                       @RequestHeader("Wechatpay-Serial") String serial,
                                       @RequestHeader("Wechatpay-Timestamp") String timestamp) {
        try {

// 构造请求参数
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(serial)
                    .nonce(nonce)
                    .signature(signature)
                    .timestamp(timestamp)
                    .body(body)
                    .build();

//            // 初始化解析器
//            NotificationParser parser = new NotificationParser(notificationConfig);

            // 验签并解密报文
            Transaction transaction = parser.parse(requestParam, Transaction.class);

            // 处理业务逻辑
            String orderNo = transaction.getOutTradeNo();
            String transactionId = transaction.getTransactionId();
            String tradeState = transaction.getTradeState().name();
            // 处理退款结果
            if ("SUCCESS".equals(transaction.getTradeState())) {

            }
//            refundService.handleRefundResult(
//                    notification.getOutRefundNo(),
//                    notification.getStatus(),
//                    notification.getAmount().getRefund()
//            );

            return "{\"code\": \"SUCCESS\", \"message\": \"成功\"}";
        } catch (Exception e) {
            return "{\"code\": \"FAIL\", \"message\": \"处理失败\"}";
        }
    }
}